---
--类名: Role.lua
--创建日期: 2015-6-15
--创建人: 陈小虎
--功能描述: 所有角色模型父类型
--修改内容：
--修改日期:   
--修改人:     
--修改备注: 
--

local Role = class("Role",function()
    return display.newNode()
end)

--角色行走线路类型
Role.LINE_1 = 1--最上面线路
Role.LINE_2 = 2--中间线路
Role.LINE_3 = 3--最下面线路

Role.DIR_LEFT = -1
Role.DIR_RIGHT = 1

Role.ACTION_MAP_IDLE = "mapidle"
Role.ACTION_MAP_MOVE = "mapmove"
Role.ACTION_MAP_WALK = "mapwalk"
Role.ACTION_MAP_MOVE_STOP = "mapmovestop"

Role.ACTION_RIDE_IDLE = "ride_idle"
Role.ACTION_RIDE_MOVE = "ride_move"
Role.ACTION_RIDE_MOVE_STOP = "ride_movestop"


local NodeGetPositionFunc = cc.Node.getPosition
local NodeSetPositionFunc = cc.Node.setPosition
local NodeGetPositionXFunc = cc.Node.getPositionX
local NodeSetPositionXFunc = cc.Node.setPositionX
local NodeIsVisibleFunc = cc.Node.isVisible
local NodeSetVisibleFunc = cc.Node.setVisible

function Role:ctor()
    self:setCascadeColorEnabled(true)
    self:setCascadeOpacityEnabled(true)
    self:setTouchSwallowEnabled(false)

	self.mUid = 0
	self.mId = 0
    self.mLine = Role.LINE_1
    self.mOldLine = self.mLine
    self.moveSpeed = Constant.MapMoveSpeed
    self.mDefaultSpeed = Constant.MapMoveSpeed
    self.mDirection = Role.DIR_LEFT
    self.mDefaultAction = Role.ACTION_MAP_IDLE

    self.mShowMinTaskId = 0

    self.mArmatrueNode = nil
    self._attachments = {}
end

function Role:isArea()
    return false
end

function Role:setShowMinTaskId(minTaskId)
    self.mShowMinTaskId = minTaskId
end

function Role:getShowMinTaskId()
    return self.mShowMinTaskId
end

function Role:setDefaultAction(action)
    self.mDefaultAction = action
end

function Role:getDefaultAction()
    return self.mDefaultAction
end

function Role:setEventVisibility(eventSet)
    self.mEventSet = eventSet
end

-- 在DoEventHandeler里设置npc是否显示
function Role:setVisibleInEvent(visible)
    self.mEventSet = false
    if visible then
        self:showSelf()
    else
        self:hideSelf()
    end
    self.mEventSet = true
end

function Role:hideSelf()
    if self.mEventSet then return end

    if NodeIsVisibleFunc(self) then
        if self.allVisibility then
            self:allVisibility(false)
        end
        -- self:hideExclamMark()
        NodeSetVisibleFunc(self,false)
    end
end

function Role:showSelf()
    if self.mEventSet then return end

    if not NodeIsVisibleFunc(self) then
        if self.allVisibility then
            self:allVisibility(true)
        end
        NodeSetVisibleFunc(self,true)
    end
end

function Role:getRootArmature()
    
end

function Role:getArmatureNode()
    return self.mArmatrueNode
end

function Role:setDefaultSpeed(defaultSpeed)
    self.mDefaultSpeed = defaultSpeed
end

function Role:getDefaultSpeed()
    return self.mDefaultSpeed
end

function Role:backToDefaultSpeed()
    self:setSpeed(self:getDefaultSpeed())
end

function Role:setSpeed(speed)
    if not speed then
        return
    end
    if self.moveSpeed ~= speed then
        self.moveSpeed = math.min(speed,Constant.MapMoveSpeedMax)
    end
end

function Role:getSpeed()
    return self.moveSpeed
end

function Role:setLine(line)
    self:setOldLine(self.mLine)
    self.mLine = line
end

function Role:getLine()
    return self.mLine
end

function Role:setOriginalLine(line)
    self.mOLine = line
end

function Role:getOriginalLine()
    return self.mOLine
end

function Role:setOldLine(line)
    self.mOldLine = line
end

function Role:getOldLine()
    return self.mOldLine
end

function Role:setId(id)
	self.mId = id
end

function Role:getId()
    return tonumber(self.mId)
end

function Role:setUid(uid)
    self.mUid = uid
end

function Role:getUid()
    return self.mUid
end

function Role:setName(name)
    self.mName = name
end

function Role:getName()
    return self.mName
end

function Role:setDirection(dir)
    self.mDirection = dir
end

function Role:getDirection()
    return self.mDirection
end

function Role:getLineMaxX()
    return MapManager:getCurMaxX(NodeGetPositionXFunc(self))
end

function Role:getLineMinX()
    return MapManager:getCurMinX(NodeGetPositionXFunc(self))
end

function Role:roundPosition(targetX)
    return MapManager:roundPosition(NodeGetPositionXFunc(self), targetX)
end

function Role:setPhysicsWorld(physicsWorld)
    self.mPhysicsWorld = physicsWorld
end

function Role:getPhysicsWorld()
	return self.mPhysicsWorld
end

function Role:setIntoSightCallback(callback)
    self._intoSightCallback = callback
end

function Role:setOutSightCallback(callback)
    self._outSightCallback = callback
end

function Role:intoSight()
    if self._intoSight then return false end
    self._intoSight = true
    self._outSight = false
    if self._intoSightCallback then
        self._intoSightCallback()
    end
    return true
end

function Role:outSight()
    if self._outSight then return false end
    self._outSight = true
    self._intoSight = false
    if self._outSightCallback then
        self._outSightCallback()
    end
    return true
end

---
--@param pos cc.p
--
function Role:showLocation(...)
    NodeSetPositionFunc(self,...)
end

function Role:getPointWorldPos()
    return 0,0
end

function Role:hideExclamMark()
    if self.mExclamMarkNode then
        self.mExclamMarkNode:stop()
        self.mExclamMarkNode:setVisible(false)
    end
end

function Role:showExclamMark(callback,params)
    if self.mExclamMarkNode then
        self.mExclamMarkNode:setVisible(true)
    end
    local times = 100000
    local action = nil
    local Armature = app:getClass("Armature")

    if params then
        self.mExclamMarkNode = Armature.create({
            path = params.dir,
            armatureName = params.name,
            animationName = "",
            skeletonName = params.name,
            textureName = params.name
        })

        action = params.action
        times = params.times
    else
        self.mExclamMarkNode = Armature.create({
            path = Res.Effect_Gantanhao.dir,
            armatureName = Res.Effect_Gantanhao.name,
            animationName = "",
            skeletonName = Res.Effect_Gantanhao.name,
            textureName = Res.Effect_Gantanhao.name
        })
        action = Res.Effect_Gantanhao.name
    end


    self:addChild(self.mExclamMarkNode)

    local x ,y = self:getPointWorldPos()
    local lx,ly = self:toLocalPosition(x ,y)
    
    NodeSetPositionFunc(self.mExclamMarkNode,lx,ly)

    local function call()
        if callback then
            if times == 1 then
                self.mExclamMarkNode:setVisible(false)
            end
            callback()
        end
    end
    
    local timesCount = 0
    local function loopCallback()
        if timesCount == 0 then
            call()
        end
        timesCount = timesCount + 1
    end
    local play
    if times == 1 then
        play = self.mExclamMarkNode:playAni({name = action,playTimes = times,completeCallback = call})--,playTimes = 10
    else
        play = self.mExclamMarkNode:playAni({name = action,playTimes = times,loopCompleteCallback = loopCallback})--,playTimes = 10
    end

    if not play then
        call()
    end

end

function Role:addAttachment(node,boneName,x,y)
    self._attachments[node] = {boneName=boneName,x=x,y=y}
    local root = self:getRootArmature()
    if root then
        root:addAttachment(node,boneName,x,y)
    end
end

function Role:removeAttachment(node)
    self._attachments[node] = nil
    local root = self:getRootArmature()
    if root then
        root:removeAttachment(node)
    end
end

function Role:reloadAttachments()
    local attachments = self._attachments
    local root = self:getRootArmature()
    if root then
        for node,att in pairs(attachments) do
            root:addAttachment(node,att.boneName,att.x,att.y)
        end
    end
end


return Role